import { Component, OnInit } from '@angular/core';
import { CommonResponse,VerificationType,ToastColor,CommonService } from '@services/common-service.service';
import { AuthTokenService } from '@services/auth-token.service'
import {HttpClient,HttpHeaders} from "@angular/common/http";
import { ToastController } from '@ionic/angular';
import { EventEmitterService,EventName } from '@services/event-emitter.service';
import { TranslateService } from '@ngx-translate/core';
import { DefaultLanguageService } from '@services/default-language.service';

export enum AppAreaTypeEnum{
  COUNTRY = 0,
	PROVINCE_STATE=1,
	CITY=2,
	COUNTY=3,
	TOWNSHIP=4
}

@Component({
  selector: 'app-billing-address',
  templateUrl: './billing-address.page.html',
  styleUrls: ['./billing-address.page.scss'],
})
export class BillingAddressPage implements OnInit {

  public loading:boolean = false;

  public appBillingAddress:any = {
    receiver:'',
    mobileNumber:'',
    addressLine1:'',
    addressLine2:'',
    city:'',
    state:'',
    country:'',
    postalCode:'',
    selectedCountry:'',
    selectedState:'',
    selectedCity:''
  };

  public countryList:Array<any> = [];

  public stateList:Array<any> = [];

  public cityList:Array<any> = [];

  private api:string = "/scm/app/getAppArea.do";

  public i18n:any;

  constructor(
    public http:HttpClient,
    private authTokenService:AuthTokenService,
    private commonService:CommonService,
    private eventEmitterService:EventEmitterService,
    private translate: TranslateService,
    private defaultLanguageService:DefaultLanguageService) { }

  ngOnInit() {
    this.initLanguage();
    this.getVoAppBillingAddress();
  }

  initLanguage(){
    this.i18n = this.commonService.initTranslate(['myAccount.setting.personal_informations.billingAddressFunction']); 
  }

  getVoAppBillingAddress(){
    var api = "/scm/app/getVoAppBillingAddress.do";
    var requestJson = {
      'appUserId':this.authTokenService.getAppUserId(),
    }
    this.http.post(api,requestJson).subscribe((response:CommonResponse) => {
      if(response.isSuccess && response.responseBody){
        this.appBillingAddress = response.responseBody;
        this.getCountryList();
        if(this.appBillingAddress.country){
          this.getStateList();
        }
        if(this.appBillingAddress.state){
          this.getCityList();
        }
      }
    },
    (errorResp)=>{
      console.log(errorResp);
    });
  }

  saveAppBillingAddress(){
    this.loading = true;
    var api = "/scm/app/saveAppBillingAddress.do";
    this.http.post(api,this.appBillingAddress).subscribe((response:CommonResponse) => {
      if(response.isSuccess){
        this.eventEmitterService.eventEmit.emit(EventName.BILLING_ADDRESS_CHANGED);
        this.commonService.commonToast(ToastColor.success,this.i18n.save_success);
      }
      this.loading = false;
    },
    (errorResp)=>{
      this.commonService.commonToast(ToastColor.warning);
      this.loading = false;
    });
  }

  getCountryList(){
    let requestJson = {
      pid:0,
      language:this.defaultLanguageService.getDefaultLanguage(),
      type:AppAreaTypeEnum.COUNTRY
    }
    this.http.post(this.api,requestJson).subscribe((response:CommonResponse) => {
      if(response.isSuccess){
        this.countryList = response.responseBody;
        this.selectedCountry();
      }
    },(errorResponse)=>{
      this.commonService.commonToast(ToastColor.warning,errorResponse.error.errorMsg);
    });
  }

  getStateList(){
    let requestJson = {
      pid:this.appBillingAddress.country,
      language:this.defaultLanguageService.getDefaultLanguage(),
      type:AppAreaTypeEnum.PROVINCE_STATE
    }
    this.http.post(this.api,requestJson).subscribe((response:CommonResponse) => {
      if(response.isSuccess){
        this.stateList = response.responseBody;
        this.selectedState();
      }
    },(errorResponse)=>{
      this.commonService.commonToast(ToastColor.warning,errorResponse.error.errorMsg);
    });
  }

  getCityList(){
    let requestJson = {
      pid:this.appBillingAddress.state,
      language:this.defaultLanguageService.getDefaultLanguage(),
      type:AppAreaTypeEnum.CITY
    }
    this.http.post(this.api,requestJson).subscribe((response:CommonResponse) => {
      if(response.isSuccess){
        this.cityList = response.responseBody;
        if(this.appBillingAddress.city){
          this.selectedCity();
        }
      }
    },(errorResponse)=>{
      this.commonService.commonToast(ToastColor.warning,errorResponse.error.errorMsg);
    });
  }

  selectedCity(){
    if(this.appBillingAddress.city){
      this.cityList.forEach((item)=>{
        if(item.id == this.appBillingAddress.city){
          this.appBillingAddress.selectedCity = item.area;
        }
      })
    }
  }

  selectedState(){
    if(this.appBillingAddress.state){
      this.stateList.forEach((item)=>{
        if(item.id == this.appBillingAddress.state){
          this.appBillingAddress.selectedState = item.area;
        }
      })
    }
  }
  
  selectedCountry(){
    if(this.appBillingAddress.country){
      this.countryList.forEach((item)=>{
        if(item.id == this.appBillingAddress.country){
          this.appBillingAddress.selectedCountry = item.area;
        }
      })
    }
  }
}
